//
//  MainTabbarController.swift
//  WeiBoSwift
//
//  Created by Jacky Sun on 16/4/18.
//  Copyright © 2016年 syf. All rights reserved.
//

import UIKit

class MainTabbarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 设置当前控制器对应tabbar颜色
        tabBar.tintColor = UIColor.orangeColor()
        
        addAllChildVC()
        
        
    }
    
    override func viewWillAppear(animated: Bool) {
      super.viewWillAppear(animated)
        // 添加加好按钮
        setUpComposeBtn()
    }
    
    private func setUpComposeBtn() {
       tabBar.addSubview(coposeBtn)
        let width = UIScreen.mainScreen().bounds.size.width / CGFloat(viewControllers!.count)
        let  rect = CGRect(x: 0, y: 0, width: width, height: 46)
        coposeBtn.frame = CGRectOffset(rect, width * 2, 1.3)
     
    }
    
    
    private func addAllChildVC() {
        // 加载data.json
        let filePath = NSBundle.mainBundle().pathForResource("sss", ofType: "json")
        // 通过文件路径 穿件nsdata
        let data = NSData.init(contentsOfFile: filePath!)
        
        do{
            let jsonObject = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)
            
          //  print(jsonObject, jsonObject.dynamicType)
            
            // 便利数组  外面的中括号代表是一个数组类型 ，里面的中括号代表是一个字典，字典key value 都是 string类型
            for dict in jsonObject as! [[String: String]]{
                addChildVC(dict["vcName"]!, title: dict["title"]!, img: dict["imageName"]!)
            }
            
        }catch{// 发生一场之后执行的代码
            print(error)
            let home = "HomeTableViewController"
            addChildVC(home, title: "首页", img: "tabbar_home")
            let discover = "DiscoverTableViewController"
            addChildVC(discover, title: "发现", img: "tabbar_discover")
            addChildVC("ZhanWViewController", title: "", img: "")
            let message = "MessageTableViewController"
            addChildVC(message, title: "消息", img: "tabbar_message_center")
            let userCenter = "UserCenterTableViewController"
            addChildVC(userCenter, title: "本地我", img: "tabbar_profile")
            
        }
 
    }
    
    
    // private 定义的方法就是私有方法
    private func addChildVC(childVCName :String, title: String, img : String){
      //  print(childVC)
        
        // 动态获取命名空间
        let ns : String =  NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
        // 将字符串转换为类
        let childVC: AnyClass? =  NSClassFromString(ns + "." + childVCName)
        // 通过类创建对象
       // 将anyclass z转换为 制定的类型
        let cls = childVC as! UIViewController.Type
        let vc = cls.init()
        
      //  print(vc)
        vc.tabBarItem.image = UIImage(named: img)
        vc.tabBarItem.selectedImage = UIImage(named: img + "_highlighted")
        vc.title = title
        
        let Nav = UINavigationController()
        Nav.addChildViewController(vc)
        
        addChildViewController(Nav)

    }
    
    
    // MARK: - 懒加载
    private lazy var coposeBtn: UIButton = {
        
       let btn = UIButton(type: UIButtonType.Custom)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
     btn.addTarget(self, action: #selector(MainTabbarController.btnClick), forControlEvents: UIControlEvents.TouchUpInside)
        return btn

    }()
    
    /**
     发送微博按钮
     */
     func btnClick() {
     let vc = SwiftComposeController()
    let nav = UINavigationController(rootViewController: vc)
    presentViewController(nav, animated: true, completion: nil)
    }

}
